package com.springboot.mall.controller;

import com.springboot.mall.common.ApiRestResponse;
import com.springboot.mall.common.Constant;
import com.springboot.mall.filter.UserFilter;
import com.springboot.mall.model.vo.CartVO;
import com.springboot.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *
 *
 *@data 2022-05-14
 */
@Controller
@RequestMapping("/cart")
public class CartController {


    @Autowired
    private CartService cartService;

    @GetMapping("/list")
    @ResponseBody
    public ApiRestResponse list(){
        //内部获取用户ID，防止横向越权
        List<CartVO> cartVOLists = cartService.list(UserFilter.currentUser.getId());
        return ApiRestResponse.success(cartVOLists);
    }

    //购物车添加商品
    @PostMapping("/add")
    @ResponseBody
    public ApiRestResponse add(@RequestParam("productId") Integer productId ,@RequestParam("count") Integer count){
        List<CartVO> add = cartService.add(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(add);

    }

    @PostMapping("/update")
    @ResponseBody
    public ApiRestResponse updateCart(@RequestParam("productId") Integer productId ,@RequestParam("count") Integer count){
        List<CartVO> cartVOS = cartService.update(UserFilter.currentUser.getId(), productId, count);
        return ApiRestResponse.success(cartVOS);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ApiRestResponse deleteCart(@RequestParam("productId") Integer productId ){
        List<CartVO> cartVOS = cartService.delete(UserFilter.currentUser.getId(), productId);
        return ApiRestResponse.success(cartVOS);

    }

    @PostMapping("/select")
    @ResponseBody
    public ApiRestResponse selectCart(@RequestParam("productId") Integer productId,@RequestParam("selected") Integer selected ){
        List<CartVO> cartVOS = cartService.updateSelectedOne(UserFilter.currentUser.getId(), productId,selected);
        return ApiRestResponse.success(cartVOS);

    }

    @PostMapping("/selectAll")
    @ResponseBody
    public ApiRestResponse selectAllCart(@RequestParam("selected") Integer selected ){
        List<CartVO> cartVOS = cartService.updateSelectedAll(UserFilter.currentUser.getId(),selected);
        return ApiRestResponse.success(cartVOS);

    }

}
